package com.athuahua.spzx.controller;

import com.athuahua.spzx.feignapi.ProductFeignClient;
import com.athuahua.spzx.service.CartService;
import com.athuahua.spzx.utils.Result;
import com.athuahua.spzx.vo.CartInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: huahua
 * @Date: 2024/8/16 0016
 * @Time: 2:31
 * @Description:
 */
@Tag(name = "购物车接口")
@RestController
@RequestMapping("/api/order/cart")
@Slf4j
public class CartController {

    @Resource
    private CartService cartService;


    /**
     * 添加购物车 修改数量+1-1
     * @param skuId  商品skuId
     * @param skuNum 商品数量
     */
    @Operation(summary = "添加购物车")
    @GetMapping("auth/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum) {
        log.info("添加购物车: skuId:{}, skuNum:{}", skuId, skuNum);
        cartService.addToCart(skuId, skuNum);

        return Result.success();
    }


    /**
     * 获取购物车列表
     */
    @Operation(summary = "获取购物车列表")
    @GetMapping("auth/cartList")
    public Result<List<CartInfoVo>> getCartList() {
        log.info("获取购物车列表");
        List<CartInfoVo> list = cartService.getCartList();
        return Result.success(list);
    }



    /**
     * 删除购物车商品
     */
    @Operation(summary = "删除购物车商品")
    @DeleteMapping("auth/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId) {
        log.info("删除购物车商品: skuId:{}", skuId);
        cartService.deleteCart(skuId);
        return Result.success();
    }

    /**
     * 更新购物车选中状态
     */
    @GetMapping("auth/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("isChecked") Integer isChecked) {
        log.info("更新购物车选中状态: skuId:{}, checked:{}", skuId, isChecked);
        cartService.checkCart(skuId, isChecked);
        return Result.success();
    }

    /**
     * 商品全选
     */
    @GetMapping("auth/allCheckCart/{isChecked}")
    public Result AllCheckCart(@PathVariable("isChecked") Integer isChecked) {
        log.info("商品全选: checked:{}", isChecked);
        cartService.AllCheckCart(isChecked);
        return Result.success();
    }

    /**
     * 清空购物车
     */
    @GetMapping("auth/clearCart")
    public Result clearCart() {
        log.info("清空购物车");
        cartService.clearCart();
        return Result.success();
    }


    /**
     * oepnfeign远程调用：返回购物车选中商品列表
     * 需要传递token参考common-service/feign
     */
    @GetMapping("auth/getAllCkecked")
    public List<CartInfoVo> getAllCkecked() {
        log.info("获取所有选中状态");
        List<CartInfoVo> list = cartService.getAllCkecked();
        return list;
    }

    /**
     * oepnfeign远程调用：删除生成订单的购物车商品
     * 需要传递token参考common-service/feign
     */
    @GetMapping("auth/deleteChecked")
    public Result deleteCheck(){
        cartService.deleteChecked();
        return Result.success();
    }

}
